var config = require('../../config/config');
import {
  queryActivity,
  luckyDraw,
  queryAwardsList
} from '../../utils/service.js'


//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    circleList: [], //圆点数组
    awardList: [], //奖品数组
    colorCircleFirst: '#FFDF2F', //圆点颜色1
    colorCircleSecond: '#F57067', //圆点颜色2
    colorAwardDefault: '#F5F0FC', //奖品默认颜色
    colorAwardSelect: '#ffe400', //奖品选中颜色
    indexSelect: 0, //被选中的奖品index
    isRunning: false, //是否正在抽奖
    imageAward: [
      '../../image/1.jpg',
      '../../image/2.jpg',
      '../../image/3.jpg',
      '../../image/4.jpg',
      '../../image/5.jpg',
      '../../image/6.jpg',
      '../../image/7.jpg',
      '../../image/8.jpg',
    ], //奖品图片数组
    activity: '',
    appId: '',
    activityId: '',
    userId: '',
    awardsList: '', 
    goldCoin:0
  },

  queryActivity: function() {
    var that = this;
    debugger
    queryActivity({
      appId: that.data.appId,
    }).then(res => {
      if (res && res.status==1) {
        that.setData({
          activityId: res.id,
          activity: res,
        });
      }
      that.queryAwardsList();
    }).catch(e => {
      console.log(e);
    })
  },
  queryAwardsList: function() {
    var that = this;
    debugger
    queryAwardsList({
      activityId: that.data.activityId,
    }).then(res => {
      if (res) {
        that.setData({
          awardsList: res,
        });
        that.getAwardList();
      }
    }).catch(e => {
      console.log(e);
    })
  },
  onLoad: function(e) {
    var userInfo = wx.getStorageSync("userInfo");
    this.setData({
      goldCoin: userInfo.goldCoin
    });
    var _this = this;
    var appId = config.appId;
    _this.setData({
      userId: e.userId,
      appId: appId
    })
    _this.queryActivity();


    //圆点设置
    var leftCircle = 7.5;
    var topCircle = 7.5;
    var circleList = [];
    for (var i = 0; i < 24; i++) {
      if (i == 0) {
        topCircle = 15;
        leftCircle = 15;
      } else if (i < 6) {
        topCircle = 7.5;
        leftCircle = leftCircle + 102.5;
      } else if (i == 6) {
        topCircle = 15
        leftCircle = 620;
      } else if (i < 12) {
        topCircle = topCircle + 94;
        leftCircle = 620;
      } else if (i == 12) {
        topCircle = 565;
        leftCircle = 620;
      } else if (i < 18) {
        topCircle = 570;
        leftCircle = leftCircle - 102.5;
      } else if (i == 18) {
        topCircle = 565;
        leftCircle = 15;
      } else if (i < 24) {
        topCircle = topCircle - 94;
        leftCircle = 7.5;
      } else {
        return
      }
      circleList.push({
        topCircle: topCircle,
        leftCircle: leftCircle
      });
    }
    this.setData({
      circleList: circleList
    })
  },
  getAwardList: function() {
    //奖品item设置
    var that = this;
    var awardsList = that.data.awardsList;
    var awardList = [];
    //间距,怎么顺眼怎么设置吧.
    var topAward = 25;
    var leftAward = 25;
    debugger
    for (var j = 0; j < awardsList.length; j++) {
      if (j == 0) {
        topAward = 25;
        leftAward = 25;
      } else if (j < 3) {
        topAward = topAward;
        //166.6666是宽.15是间距.下同
        leftAward = leftAward + 166.6666 + 15;
      } else if (j < 5) {
        leftAward = leftAward;
        //150是高,15是间距,下同
        topAward = topAward + 150 + 15;
      } else if (j < 7) {
        leftAward = leftAward - 166.6666 - 15;
        topAward = topAward;
      } else if (j < 8) {
        leftAward = leftAward;
        topAward = topAward - 150 - 15;
      }
      var imageAward = this.data.imageAward[j];
      var award = awardsList[j];

      awardList.push({
        topAward: topAward,
        leftAward: leftAward,
        imageAward: award.imgUrl,
        id: award.id,
        name: award.name,
        index: j
      });
    }
    this.setData({
      awardList: awardList
    })
  },
  //开始游戏
  startGame: function() {
    var that = this;
    if (that.data.isRunning) return
    that.setData({
      isRunning: true
    })
    luckyDraw({
      activityId: that.data.activityId,
      userId: that.data.userId
    }).then(res => {
      debugger
      if (res.result == false) {
        wx.showModal({
          title: '错误',
          content: res.errorMessage,
          showCancel: false, //去掉取消按钮
          success: function(res) {
            if (res.confirm) {}
          }
        })
        _this.setData({
          isRunning: false
        })
      } else {
        
        if (that.data.goldCoin>=200){
          that.setData({
            goldCoin: that.data.goldCoin-200
          })
        }
        that.setData({
          activityinfo: res
        })
        that.luckyDraw();
      }

    }).catch(e => {

    })

  },

  //幸运抽奖
  luckyDraw: function(timer) {
    var _this = this;
    var indexSelect = 0
    var i = 0;
    //圆点闪烁
    var circletimer = setInterval(function() {
      if (_this.data.colorCircleFirst == '#FFDF2F') {
        _this.setData({
          colorCircleFirst: '#F57067',
          colorCircleSecond: '#FFDF2F',
        })
      } else {
        _this.setData({
          colorCircleFirst: '#FFDF2F',
          colorCircleSecond: '#F57067',
        })
      }
    }, 500)
    _this.setData({
      circletimer: circletimer
    })
    debugger
    var timer = setInterval(function() {
      indexSelect++;
      //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度
      i += 30;
      var activityinfo = _this.data.activityinfo;
      //500抽奖时间 
      debugger
      if (i > 500 && activityinfo) {
        var sort = activityinfo.sort
        indexSelect = indexSelect % 8;
        if (indexSelect == sort - 1) {
          debugger
          clearInterval(timer)
          clearInterval(_this.data.circletimer) 
          if (activityinfo.type==1){
            _this.setData({ 
              goldCoin: _this.data.goldCoin + activityinfo.value
            })
          }
          _this.setData({
            isRunning: false, 
          })
          
          wx.showModal({
            title: '恭喜您',
            content: '获得了' + (activityinfo.value) + "个" + (activityinfo.name),
            showCancel: false, //去掉取消按钮
            success: function(res) {
              if (res.confirm) {}
            }
          })
        }
        _this.setData({
          indexSelect: indexSelect
        })
        //去除循环
      } else {
        indexSelect = indexSelect % 8;
        _this.setData({
          indexSelect: indexSelect
        })
      }
      //达到一千的时候关闭 防止一直转动
      if (i > 1000) {
        clearInterval(timer)
        clearInterval(_this.data.circletimer)
        _this.setData({
          isRunning: false 
        })
      }


    }, (200 + i))

  },
})